<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a href="javascript:;" v-for="item in filterData.brands" :key="item.id" :class="{ active: item.id === filterData.brands.selectedBrand }" @click="changeBrand(item.id)">{{ item.name }}</a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}</div>
      <div class="body">
        <a href="javascript:;" v-for="i in item.properties" :key="i.id" :class="{ active: i.id === item.selectedProp }" @click="changeProp(item, i.id)">{{ i.name }}</a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  setup(props, { emit }) {
    const route = useRoute()
    const filterData = ref(null)
    const filterLoading = ref(false)
    // 分类发生变化时候需要重新获取筛选数据，需要侦听器
    watch(
      () => route.params.id,
      (newValue) => {
        // 什么时候应该去获取数据，当处于二级分类的时候才能去获取数据，当我处于一级分类的时候，不能去请求数据
        if (newValue && route.path === `/category/sub/${newValue}`) {
          filterLoading.value = true
          findSubCategoryFilter(route.params.id).then((data) => {
            // 每一组可以筛选的条件缺失全部条件，处理数据之后加上全部
            data.result.brands.unshift({ id: null, name: '全部' })
            // 选中的品牌，选中的属性
            data.result.brands.selectedBrand = null
            //  属性
            data.result.saleProperties.forEach((item) => {
              // 选中的属性
              item.selectedProp = null
              item.properties.unshift({ id: null, name: '全部' })
            })
            filterData.value = data.result
            filterLoading.value = false
            console.log(filterData)
          })
        }
      },
      { immediate: true }
    )

    // 获取筛选参数的组件
    const getFilterParams = () => {
      // 参考数据  {brandId:'', attrs:[{groupName:'',propertyName:''}...]}
      const obj = { brandId: null, attrs: [] }
      // 品牌
      obj.brandId = filterData.value.brands.selectedBrand
      // 参考数据
      filterData.value.saleProperties.forEach((item) => {
        if (item.selectedProp) {
          const prop = item.properties.find((prop) => {
            return prop.id === item.selectedProp
          })
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }

    // 记录当前的品牌
    const changeBrand = (brandId) => {
      if (filterData.value.brands.selectedBrand === brandId) return
      filterData.value.brands.selectedBrand = brandId
      emit('filter-change', getFilterParams())
    }

    // 记录当前选择的销售属性
    const changeProp = (item, propId) => {
      if (item.selectedProp === propId) return
      item.selectedProp = propId
      emit('filter-change', getFilterParams())
    }

    return { filterData, filterLoading, changeBrand, changeProp, getFilterParams }
  }
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
  .xtx-skeleton {
    padding: 10px 0;
  }
}
</style>
